package test1;

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) {
        try {
            // 创建服务器端的ServerSocket，监听端口 12345
            ServerSocket serverSocket = new ServerSocket(12345);
            System.out.println("服务器启动，等待客户端连接...");

            // 接受客户端连接
            Socket clientSocket = serverSocket.accept();
            System.out.println("客户端已连接");

            // 获取客户端的输入流和输出流
            BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);

            // 接收客户端发送的消息
            String clientMessage = input.readLine();
            System.out.println("收到客户端消息: " + clientMessage);

            // 发送响应消息给客户端
            String responseMessage = "服务器已收到消息: " + clientMessage;
            output.println(responseMessage);
            System.out.println("已发送响应给客户端");

            // 关闭连接
            clientSocket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

